﻿Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Drawing.Design
Imports System.Security.Permissions
Imports System.Windows.Forms

Namespace System.Workflow.ComponentModel.Design
    <PermissionSet(SecurityAction.LinkDemand, Name:="FullTrust"), PermissionSet(SecurityAction.InheritanceDemand, Name:="FullTrust")> _
    Public Class BindUITypeEditor
        Inherits UITypeEditor
        ' Methods
        Friend Shared Function EditValue(ByVal context As ITypeDescriptorContext) As Object
            Dim obj2 As Object = Nothing
            If (((Not context Is Nothing) AndAlso (Not context.PropertyDescriptor Is Nothing)) AndAlso (Not context.Instance Is Nothing)) Then
                Dim editor As New BindUITypeEditor
                obj2 = context.PropertyDescriptor.GetValue(context.Instance)
                obj2 = editor.EditValue(context, context, obj2)
                Try 
                    context.PropertyDescriptor.SetValue(context.Instance, obj2)
                Catch exception As Exception
                    Dim message As String = SR.GetString("Error_CanNotBindProperty", New Object() { context.PropertyDescriptor.Name })
                    If Not String.IsNullOrEmpty(exception.Message) Then
                        message = (message & ChrW(10) & ChrW(10) & exception.Message)
                    End If
                    DesignerHelpers.ShowError(context, message)
                End Try
            End If
            Return obj2
        End Function

        Public Overrides Function EditValue(ByVal context As ITypeDescriptorContext, ByVal serviceProvider As IServiceProvider, ByVal value As Object) As Object
            If (serviceProvider Is Nothing) Then
                Throw New ArgumentNullException("serviceProvider")
            End If
            Me.serviceProvider = serviceProvider
            Dim binding As Object = value
            If ((Not context Is Nothing) AndAlso TypeOf context.PropertyDescriptor Is DynamicPropertyDescriptor) Then
                Try 
                    Dim form As New ActivityBindForm(Me.serviceProvider, context)
                    If (DialogResult.OK <> form.ShowDialog) Then
                        Return binding
                    End If
                    If form.CreateNew Then
                        If form.CreateNewProperty Then
                            Dim customProperties As List([Of] CustomProperty) = CustomActivityDesignerHelper.GetCustomProperties(context)
                            If (Not customProperties Is Nothing) Then
                                customProperties.Add(CustomProperty.CreateCustomProperty(Me.serviceProvider, form.NewMemberName, context.PropertyDescriptor, context.Instance))
                                CustomActivityDesignerHelper.SetCustomProperties(customProperties, context)
                            End If
                        Else
                            ActivityBindPropertyDescriptor.CreateField(context, form.Binding, True)
                        End If
                    End If
                    binding = form.Binding
                Catch exception As Exception
                    Dim message As String = SR.GetString("Error_CanNotBindProperty", New Object() { context.PropertyDescriptor.Name })
                    If Not String.IsNullOrEmpty(exception.Message) Then
                        message = (message & ChrW(10) & ChrW(10) & exception.Message)
                    End If
                    DesignerHelpers.ShowError(context, message)
                End Try
                Return binding
            End If
            DesignerHelpers.ShowError(Me.serviceProvider, SR.GetString("Error_MultipleSelectNotSupportedForBindAndPromote"))
            Return binding
        End Function

        Public Overrides Function GetEditStyle(ByVal typeDescriptorContext As ITypeDescriptorContext) As UITypeEditorEditStyle
            Return UITypeEditorEditStyle.Modal
        End Function


        ' Fields
        Private Const MaxItems As Integer = 10
        Private serviceProvider As IServiceProvider
    End Class
End Namespace

